#!/usr/bin/python
# -*- coding: utf-8 -*-


import wx
import wx.calendar
import re
import os

#Subir sin clave

def mensageOkCancel(parent,msg):

    dlg = wx.MessageDialog(parent, msg, 'Confirmación',
                               wx.OK 
                               | wx.ICON_INFORMATION 
                               | wx.CANCEL
                               #| wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                               )

    if dlg.ShowModal() == wx.ID_OK:
        return True
    
    return False


def mensageError(parent,msg):
    dlg = wx.MessageDialog(parent, msg, 'ERROR',
                               wx.OK 
                               #| wx.ICON_INFORMATION 
                               | wx.ICON_ERROR
                               #| wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                               )
    dlg.ShowModal()
    dlg.Destroy()
    
def mensageAlerta(parent,msg):
    dlg = wx.MessageDialog(parent, msg,'CONFIRMACIÓN',
                               wx.OK | wx.ICON_INFORMATION #wx.ICON_ERROR
                               #wx.YES_NO | wx.NO_DEFAULT | wx.CANCEL | wx.ICON_INFORMATION
                               )
    dlg.ShowModal()
    dlg.Destroy()

def mensajePregunta(parent, msg):
        dlg = wx.MessageDialog(parent, msg, 'Pregunta', 
            wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
        
        val = dlg.ShowModal()
        if val == wx.ID_CANCEL:
            return False
        if val == wx.ID_YES:
            return True #self.OnSave(evt) #well, the parameter is unused

        
        dlg.Destroy()

def validarNombreFichero(nombre):
    
    #Se realiza la validadcion
    p = re.compile('.*(\.|,|\?|¿|!|\s|#|@|%|\$|\'|").*')
    if p.match(nombre):
        return False
    else:
        return True

def validarNombreCategoria(parent,nombre):
    p = re.compile('.*(\.|,|\?|¿|!|\s|#|@|%|\$|\'|"|\\|/|\)|\(|=|&).*')
    if p.match(nombre):
        mensageError(parent,"El nombre de la categoría no puede contener:\n\n"
                                        + "1) Espacios en blanco\n"
                                        + "2) Caracteres especiales !\"\#$%&/()=?¿!.,"
                                        )
        return False
        
    return True
        
def validarClave(clave):
    
    
    #Validar clave
    if clave == "":
        return False

    #if re.compile(r"(.*\..*)",re.IGNORECASE)
    
    return True


def verificarCorreo(correo):

    p = re.compile(r"(?:^|\s)[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)",re.IGNORECASE)
    return p.match(correo)

def extraerCorreoUID(uid):

    p = re.compile(r"(?:^|\s)[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)",re.IGNORECASE)
    return p.match(correo)
    

#PasswordEntryDialog
def dialogEntrada(parent,msj,password=False):
    if not password:
        while(True):
            dlg = wx.TextEntryDialog(parent, msj,'Tibisay',style=wx.OK | wx.CANCEL)
            if dlg.ShowModal() == wx.ID_OK:
                if dlg.GetValue() != "":
                    break
            
                dlg.Destroy()
            else:
                break

        #dlg.Destroy()
    else:
        while(True):
            dlg = wx.PasswordEntryDialog(parent, msj,'Tibisay',style=wx.OK| wx.CANCEL)
            #dlg.SetValue("Python is the best!")
            if dlg.ShowModal() == wx.ID_OK:
                if dlg.GetValue() != "":
                    break
                
                dlg.Destroy()
            else:
                break

    dlg.Destroy()    
    return dlg.GetValue()

def getUrl(vista):
    
    dlg = wx.DirDialog(vista, "Seleccione el directorio donde alamcenará los datos:",
                          style=wx.DD_DEFAULT_STYLE
                           #| wx.DD_DIR_MUST_EXIST
                           #| wx.DD_CHANGE_DIR
                           )

    dlg.SetPath(os.environ['HOME'])

    if dlg.ShowModal() == wx.ID_OK:
        return dlg.GetPath()

    else:
        return False    
        
    dlg.Destroy()
    

class curry(object):
    """Taken from the Python Cookbook, this class provides an easy way to
    tie up a function with some default parameters and call it later.
    See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549 for more.
    """
    def __init__(self, func, *args, **kwargs):
        self.func = func
        self.pending = args[:]
        self.kwargs = kwargs

    def __call__(self, *args, **kwargs):
        if kwargs and self.kwargs:
            kw = self.kwargs.copy()
            kw.update(kwargs)
        else:
            kw = kwargs or self.kwargs
        return self.func(*(self.pending + args), **kw)


class dropArgs(object):
    """ Same as curry, but once the function is built, further args are ignored. """

    def __init__(self, func, *args, **kwargs):
        self.func = func
        self.args = args[:]
        self.kwargs = kwargs

    def __call__(self, *args, **kwargs):
        return self.func(*self.args, **self.kwargs)
